package com.xjl.shujujiegou;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @描述： 用队列实现栈
 * @作者： 许JIAN林
 */
public class QueueStack {
    private Queue<Integer> data;
    private Queue<Integer> help;

    public QueueStack() {
        data = new LinkedList<Integer>();
        help = new LinkedList<Integer>();
    }

    public void push(int num){
        data.offer(num);
    }

    public Integer pop() {

        while(data.size() > 1) {
            help.offer(data.poll());
        }
        int res = data.poll();
        swap();
        return res;
    }

    public Integer peek() {
        if (data.size() == 0) {
            return null;
        }

        while( data.size() > 1) {
            help.offer(data.poll());
        }
        int res = data.peek();
        help.offer(data.poll());
        swap();
        return res;
    }

    private void swap() {
        Queue<Integer> tmp = new LinkedList<>();
        tmp = data;
        data = help;
        help = tmp;


    }
}
